package lee.android.dafuwon;

import lee.android.factory.ButtonFactory;
import lee.base.constant.Symbol;
import lee.base.constant.XmlAttribute;
import lee.base.structure.BoundingBox;
import lee.base.structure.MyHashTree;
import lee.base.structure.MyNode;
import lee.base.xml.ContentParser;
import lee.base.xml.XmlReader;
import lee.base.xmlformat.AttrValImageItem;
import lee.base.xmlformat.FrameFormat;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.ImageButton;

public class GameMode1 extends Activity {

	String filePathRoot = null;
	MyHashTree treeLayoutInfo = null;
	ContentParser cp = null;
	int frameWidth = 0, frameHeight = 0;
	AbsoluteLayout mainLayout = null;
	boolean curScreenType = Symbol.SCREEN_VERTICAL;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initialize variable
        this._001_initialVariable();
        //read some necessary file
        this._002_readXmlFile();
        //build component based on tree data
        this._003_buildFrameComponent();
    }
    
    private void _001_initialVariable() {
    	//set filePath
    	this.filePathRoot = Environment.getExternalStorageDirectory().toString();
    	//create ContentParser object
    	this.cp = new ContentParser();
    	//set width and length of screen
    	DisplayMetrics dm = new DisplayMetrics();
    	this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    	this.frameWidth = dm.widthPixels;
    	this.frameHeight = dm.heightPixels;
    	if(this.frameWidth < this.frameHeight) {
    		int temp = this.frameWidth;
    		this.frameWidth = this.frameHeight;
    		this.frameHeight = temp;
    	}
    	//set full screen
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN, WindowManager.LayoutParams. FLAG_FULLSCREEN);
    }
    
    private void _002_readXmlFile() {
    	//get layout information for this frame
    	this.treeLayoutInfo = new XmlReader(this.filePathRoot + "/FrameMainMode1/Frame_GameMain.xml").GET001_MyHashTree();
    	//set initial value to tree parameter
    	this.treeLayoutInfo.ACT004_setSpecificValue("$ref1@width", this.frameWidth + "");
    	this.treeLayoutInfo.ACT004_setSpecificValue("$ref1@height", this.frameHeight + "");
    	this.treeLayoutInfo.ACT004_setSpecificValue("$ref1@root_dir", this.filePathRoot + "/FrameMainMode1");
    	//parse layout information tree
    	this.cp.ACT001_parseFullTree(this.treeLayoutInfo);
    }
    
    @SuppressWarnings("deprecation")
	private void _003_buildFrameComponent() {
    	//set layout
    	this.mainLayout = new AbsoluteLayout(this);
    	//test image button
    	this._004_setItemButtonMainSelection();
    	this._005_setItemButtonPlayerState();
    	this._006_setItemButtonSkillTree();
    	this._007_setItemButtonItemList();
    	this._008_setItemMarquee();
    	this._009_setItemDice_column();
    	this._010_setItemButtonThrowDice();
    	//set view
    	this.setContentView(this.mainLayout);
    }
    
    private void _004_setItemButtonMainSelection() {
    	this.mainLayout.addView(this._011_getImageButton("main_selection"));
    }
    
    private void _005_setItemButtonPlayerState() {
    	this.mainLayout.addView(this._011_getImageButton("player_state"));
    }
    
    private void _006_setItemButtonSkillTree() {
    	this.mainLayout.addView(this._011_getImageButton("skill_tree"));
    }
    
    private void _007_setItemButtonItemList() {
    	this.mainLayout.addView(this._011_getImageButton("item_list"));
    }
    
    private void _008_setItemMarquee() {
    	
    }
    
    private void _009_setItemDice_column() {
    	this.mainLayout.addView(this._011_getImageButton("dice_column"));
    }
    
    private void _010_setItemButtonThrowDice() {
    	this.mainLayout.addView(this._011_getImageButton("throw_dice"));
    }
    
    private ImageButton _011_getImageButton(String itemName) {
    	String splitPoint = XmlAttribute.VARIABLE_POINT + "";
    	MyNode nodeHead = this.treeLayoutInfo.ACT003_getSpecificNode("$ref2" + splitPoint + itemName, null);
    	AttrValImageItem avii = FrameFormat.getImageItem(this.treeLayoutInfo, nodeHead, this.curScreenType);
    	ImageButton btn = ButtonFactory.getImageButton(avii.fileNameImage, this, new BoundingBox(avii.pos.x, avii.pos.y, avii.size.width, avii.size.height));
    	return btn;
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        	this.curScreenType = Symbol.SCREEN_HORIZENTAL;
            this._003_buildFrameComponent();
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        	this.curScreenType = Symbol.SCREEN_VERTICAL;
        	this._003_buildFrameComponent();
        }
    }
}
